﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HdipCricketClub.Models;
using HdipCricketClub.ViewModels;

namespace HdipCricketClub.Controllers
{ 
    public class ReviewTeamController : Controller
    {
        private CricketClubEntities db = new CricketClubEntities();

        FixtureRepository fixtureRepository = new FixtureRepository();
        MemberRepository memberRepository = new MemberRepository();
        ClubFixturePlayerRepository clubFixturePlayerRepository = new ClubFixturePlayerRepository();

        //
        // GET: /ReviewTeam/

        public ViewResult Index()
        {
            var fixtures = fixtureRepository.FindFixturesWithNoScoreCard();

            if (fixtures.Count() == 0)
            {
                ViewBag.Message = "There are no unplayed fixtures where a team has not been selected";
            }
      
            return View(fixtures.ToList());
        }
        
        //
        // GET: /ReviewTeam/GetTeam/

        public ViewResult GetTeam(int id)
        {
            //EditTeamViewModel editTeamViewModel = new EditTeamViewModel();

            //editTeamViewModel.ClubFixturePlayers = clubFixturePlayerRepository.GetPlayers(id);
            //editTeamViewModel.Players = clubFixturePlayerRepository.GetPlayers(id);

            
            var clubFixturePlayers = clubFixturePlayerRepository.GetPlayers(id);
            
            var players = memberRepository.FindAllPlayers();

            ViewBag.player_id = new SelectList(db.Members, "member_id", "FirstAndLastName");
            var fixture = db.Fixtures.First(f => f.fixture_id == id);
            ViewBag.team = fixture.Team.name;
            ViewBag.opponentClub = fixture.Opponent.club_name;
            ViewBag.opponentTeam = fixture.Opponent.team_name;
            ViewBag.date = fixture.date;

            return View(clubFixturePlayers.ToList());
        }
        

        //
        // GET: /ReviewTeam/Details/5

        public ViewResult Details(int id)
        {
            ClubFixturePlayer clubfixtureplayer = db.ClubFixturePlayers.Single(c => c.fixture_id == id);
            return View(clubfixtureplayer);
        }

        //
        // GET: /ReviewTeam/Create

        public ActionResult Create()
        {
            ViewBag.fixture_id = new SelectList(db.Fixtures, "fixture_id", "type");
            ViewBag.player_id = new SelectList(db.Members, "member_id", "first_name");
            ViewBag.how_out = new SelectList(db.DismissalTypes, "dismissal_id", "type");
            return View();
        } 

        //
        // POST: /ReviewTeam/Create

        [HttpPost]
        public ActionResult Create(ClubFixturePlayer clubfixtureplayer)
        {
            if (ModelState.IsValid)
            {
                db.ClubFixturePlayers.AddObject(clubfixtureplayer);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.fixture_id = new SelectList(db.Fixtures, "fixture_id", "type", clubfixtureplayer.fixture_id);
            ViewBag.player_id = new SelectList(db.Members, "member_id", "first_name", clubfixtureplayer.player_id);
            ViewBag.how_out = new SelectList(db.DismissalTypes, "dismissal_id", "type", clubfixtureplayer.how_out);
            return View(clubfixtureplayer);
        }
        
        //
        // GET: /ReviewTeam/Edit/5
 
        public ActionResult Edit(int id)
        {
            ClubFixturePlayer clubfixtureplayer = db.ClubFixturePlayers.Single(c => c.fixture_id == id);
            ViewBag.fixture_id = new SelectList(db.Fixtures, "fixture_id", "type", clubfixtureplayer.fixture_id);
            ViewBag.player_id = new SelectList(db.Members, "member_id", "first_name", clubfixtureplayer.player_id);
            ViewBag.how_out = new SelectList(db.DismissalTypes, "dismissal_id", "type", clubfixtureplayer.how_out);
            return View(clubfixtureplayer);
        }

        //
        // POST: /ReviewTeam/Edit/5

        [HttpPost]
        public ActionResult Edit(ClubFixturePlayer clubfixtureplayer)
        {
            if (ModelState.IsValid)
            {
                db.ClubFixturePlayers.Attach(clubfixtureplayer);
                db.ObjectStateManager.ChangeObjectState(clubfixtureplayer, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.fixture_id = new SelectList(db.Fixtures, "fixture_id", "type", clubfixtureplayer.fixture_id);
            ViewBag.player_id = new SelectList(db.Members, "member_id", "first_name", clubfixtureplayer.player_id);
            ViewBag.how_out = new SelectList(db.DismissalTypes, "dismissal_id", "type", clubfixtureplayer.how_out);
            return View(clubfixtureplayer);
        }

        //
        // GET: /ReviewTeam/Delete/5
 
        public ActionResult Delete(int id)
        {
            ClubFixturePlayer clubfixtureplayer = db.ClubFixturePlayers.Single(c => c.fixture_id == id);
            return View(clubfixtureplayer);
        }

        //
        // POST: /ReviewTeam/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            ClubFixturePlayer clubfixtureplayer = db.ClubFixturePlayers.Single(c => c.fixture_id == id);
            db.ClubFixturePlayers.DeleteObject(clubfixtureplayer);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}